﻿namespace BEGDC.CC.Web.UserControls.EBDCV
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using BEGDC.CC.BL.CCFlow.EBDCV;
	using BEGDC.CC.Entity;
	using BEGDC.CC.BL.Workflow;
	using BEGDC.CC.BL.Common;
	using BEGDC.CC.BL.Common.Query;
	using BEGDC.Utility.Castle;

	/// <summary>
	///		Summary description for CATPSOApprove.
	/// </summary>
	public class CostCenterHeadApproval : BaseEBDCV
	{
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Button btnReject;
		protected System.Web.UI.WebControls.Button btnSubmit;
		protected System.Web.UI.WebControls.Button btnReply;
        protected System.Web.UI.WebControls.Button btnClose;
		protected System.Web.UI.WebControls.TextBox tbOperationFlag;
		protected System.Web.UI.WebControls.TextBox tbOperationContent;
		protected System.Web.UI.WebControls.TextBox tbComment;
		protected EBDCVCtrl uclEBDCV;
		//protected BEGDC.CC.Web.UserControls.CV.CVPrintCtrl cvPrintCtrl;

		private bool IsQuery
		{
			get
			{
				string state = this.Request.QueryString["State"];
				if(state == null) return false;
				return state == SysDicItem.ProjectStatusFlag.QUERY;
			}
		}

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(!Page.IsPostBack)
			{
				uclEBDCV.isReadOnly = true;
				this.btnReply.Attributes.Add("onclick", "javascript:OpenReplyPane("+Request.QueryString["FlowID"]+");");
				this.btnReject.Attributes.Add("onclick", "javascript:OpenCommentPane();");
				this.btnSubmit.Attributes.Add("onclick", "javascript:OnSubmit();");
                this.btnClose.Attributes.Add("onclick", "javascript:SimpleCloseWindow();");
			}
			ButtonStatusSetting();
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnReply.Click += new EventHandler(this.btnReply_Click);
			this.btnSubmit.Click += new EventHandler(this.btnSubmit_Click);
			this.btnReject.Click += new EventHandler(this.btnReject_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnReject_Click(object sender, EventArgs e)
		{
			try
			{
				IEBDCV ocrService = AppContainer.Instance[ServiceConst.EBDCVService] as IEBDCV;
				ocrService.DepartmentHeadApprove(Convert.ToInt64(Request.QueryString["FlowID"]), CVTransitionList.Reject, WebUtility.DeCodeString(Server, tbComment.Text));
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void btnSubmit_Click(object sender, EventArgs e)
		{
			try
			{
				IEBDCV ocrService = AppContainer.Instance[ServiceConst.EBDCVService] as IEBDCV;
				ocrService.CostCenterHeadApprove(Convert.ToInt64(Request.QueryString["FlowID"]), EBDCVTransitionList.Approve, string.Empty);
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
			}
			catch(Exception exp)
			{
				if (exp.InnerException !=null)
				
					Page.RegisterClientScriptBlock("","<script>alert('" + exp.InnerException.Message + "');</script>");
				else
					Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}

		private void ControlStatusSetting(Accessibility access )
		{
			if( access == Accessibility.Readonly )
			{
				this.DisableChildrenControls(this);
			}
		}

		private void Btn_Export_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			try
			{
				ExcelTool.ICCExcelExport excelExport=new ExcelTool.ExportCVForm();
				excelExport.Export(this.Page, this.AppID );
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');</script>");
			}
			catch (BEGDC.Utility.ExceptionManagement.GDCException exp)
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.ErrorMessage.Export_0001 + "');</script>");
			}

		}

		protected override void ButtonStatusSetting()
		{			
			if( this.Access == null )
				return;

			this.btnSubmit.Visible = this.Access.IsButtonVisible(CVTransitionList.Approve);
			this.btnReject.Visible = this.Access.IsButtonVisible(CVTransitionList.Reject);
			this.btnReply.Visible = this.Access.IsButtonVisible(CVTransitionList.Reply);
            this.uclEBDCV.cvPrintCtrl.ProcessType = WorkflowTypes.EBDCV;
            this.uclEBDCV.cvPrintCtrl.Visible = this.Access.IsButtonVisible(EBDCVTransitionList.Copy);
		}

		private void btnReply_Click(object sender, EventArgs e)
		{
			try
			{
				IEBDCV ocrService = AppContainer.Instance[ServiceConst.EBDCVService] as IEBDCV;
				if(this.tbOperationFlag.Text == "reply")
					ocrService.ReplyQuery(Convert.ToInt64(Request.QueryString["FlowID"]), WebUtility.DeCodeString(Server, tbComment.Text));
				
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CV001 + "');</script><script>window.close();</script>");
			}
			catch
			{
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CV002 + "');</script>");
			}
		}
	}
}
